package com.android.lovingpet.community.posting;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.lovingpet.R;
import com.android.lovingpet.base.BaseActivity;
import com.android.lovingpet.bean.Account;
import com.android.lovingpet.bean.Post;
import com.android.lovingpet.bean.Team;
import com.android.lovingpet.common.teaminfo.TeamInfoActivity;
import com.android.lovingpet.common.userinfo.UserInfoActivity;
import com.android.lovingpet.util.Utility;
import com.bumptech.glide.Glide;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

public class PostingActivity extends BaseActivity<PostingContract.View, PostingPresenter> implements PostingContract.View, View.OnClickListener, XRecyclerView.LoadingListener {

    private List<Post> comments;
    private CommentAdapter adapter;
    private XRecyclerView rvComment;
    private EditText etComment;
    private Post post;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_posting);
        comments = new ArrayList<>();
        adapter = new CommentAdapter(comments);
        rvComment = findViewById(R.id.rv_comment);
        rvComment.setLayoutManager(new LinearLayoutManager(this));
        rvComment.setNestedScrollingEnabled(false);
        rvComment.setPullRefreshEnabled(false);
        rvComment.setAdapter(adapter);
        rvComment.setLoadingListener(this);
        etComment = findViewById(R.id.et_comment);

        post = getIntent().getParcelableExtra("post");
        ImageView ivHead = findViewById(R.id.iv_head);
        TextView tvName = findViewById(R.id.tv_name);
        if (post.getUserType() == 1) {
            Account account = Utility.getAccount(post.getUserId());
            Glide.with(this).load(account.getHead()).into(ivHead);
            tvName.setText(account.getName());
            ivHead.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(PostingActivity.this, UserInfoActivity.class);
                    intent.putExtra("account", account);
                    startActivity(intent);
                }
            });
        } else {
            Team team = Utility.getTeam(post.getUserId());
            Glide.with(this).load(team.getImage()).into(ivHead);
            tvName.setText(team.getName());
            ivHead.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(PostingActivity.this, TeamInfoActivity.class);
                    intent.putExtra("team", team);
                    startActivity(intent);
                }
            });
        }
        setInfo();
        if (getIntent().getBooleanExtra("isComment", false)) {
            toComment();
        }
        if (post.getCommentCount() > 0) {
            getPresenter().getComments(post.getRealId(), 0);
        }
        findViewById(R.id.bt_send).setOnClickListener(this);
        findViewById(R.id.iv_comment).setOnClickListener(this);
    }

    @Override
    public PostingPresenter createPresenter() {
        return new PostingPresenter();
    }

    @Override
    public PostingContract.View createView() {
        return this;
    }

    @Override
    public void addComment(Post comment) {
        comments.add(comment);
        runOnUiThread(() -> adapter.notifyDataSetChanged());
    }

    @Override
    public void addComments(List<Post> comments) {
        this.comments.addAll(comments);
        runOnUiThread(() -> adapter.notifyDataSetChanged());
    }

    @Override
    public void addCommentInfo() {
        runOnUiThread(() -> adapter.notifyDataSetChanged());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_send:
                getPresenter().comment(etComment.getText().toString().trim(), post.getRealId());
                break;

            case R.id.iv_comment:
                toComment();
                break;

            default:
                break;
        }
    }

    @Override
    public void onRefresh() {

    }

    @Override
    public void onLoadMore() {
        getPresenter().getComments(post.getRealId(), comments.size());
    }

    @Override
    public void loadMoreComplete() {
        runOnUiThread(() -> rvComment.loadMoreComplete());
    }

    @Override
    public void clearText() {
        runOnUiThread(() -> {
            etComment.getText().clear();
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);
        });
    }

    private void setInfo() {
        ((TextView) findViewById(R.id.tv_title)).setText("动态详情");
        TextView tvTime = findViewById(R.id.tv_variety);
        TextView tvContext = findViewById(R.id.tv_context);
        RecyclerView rvAlbum = findViewById(R.id.rv_album);
        tvTime.setText(post.getCreateTime());
        tvContext.setText(post.getContext());
        rvAlbum.setLayoutManager(new GridLayoutManager(this, 3));
        rvAlbum.setNestedScrollingEnabled(false);
        rvAlbum.setAdapter(new AlbumAdapter(post.getImages()));
    }

    private void toComment() {
        etComment.setFocusable(true);
        etComment.setFocusableInTouchMode(true);
        etComment.requestFocus();
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(etComment, 0);
    }
}
